import config from '../config'
interface PositionType {
  x: number
  y: number
}
class Position {
  collection: Array<PositionType> = []
  // 随机产生坐标
  public position() {
    return {
      x: Math.floor(Math.random() * (config.canvas.width / config.model.width)) * config.model.width,
      y:
        Math.floor(Math.random() * (config.canvas.height / config.model.height - 5)) * config.model.height +
        config.model.height * 2
    }
  }
  // 批量获取唯一坐标
  public positionCollection(num: number) {
    const collection: Array<{ x: number; y: number }> = []
    for (let i = 0; i < num; i++) {
      const flag = true
      while (flag) {
        const position = this.position()
        const exists = this.collection.some((c) => c.x === position.x && c.y === position.y)
        if (!exists) {
          collection.push(position)
          this.collection.push(position)
          break
        }
      }
    }
    return collection
  }
}
export default new Position()
